package io.seata.factoryservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.seata.factoryservice.entity.Nahco3Material;
import io.seata.factoryservice.entity.NaohMaterial;
import io.seata.factoryservice.entity.Nh3Material;
import io.seata.factoryservice.entity.ProductionPlan;
import io.seata.factoryservice.mapper.ProductionPlanMapper;
import io.seata.factoryservice.service.ProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : author
 * @create 2022/8/19 15:57
 */
@Service
public class ProductionPlanServiceImpl implements ProductionPlanService {

    @Autowired
    private ProductionPlanMapper productionPlanMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public void addProductionPlan(ProductionPlan productionPlan) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        productionPlanMapper.insert(productionPlan);
        if (productionPlan.getPpquick() == 0) {
            opsForHash.put("findProductionPlanByNoQuick", String.valueOf(productionPlan.getPpid()), productionPlan);
        } else {
            opsForHash.put("findProductionPlanByQuick", String.valueOf(productionPlan.getPpid()), productionPlan);
        }
    }

    @Override
    public List<ProductionPlan> findProductionPlanByQuick() {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
//        从缓存读取数据
        List<Object> findProductionPlanByQuick = opsForHash.values("findProductionPlanByQuick");
//        数据类型转化
        List<ProductionPlan> newList = new ArrayList<ProductionPlan>();
        for (Object object : findProductionPlanByQuick) {
            newList.add((ProductionPlan) object);
        }
//        判断是否查询数据库
        if (!newList.isEmpty()) {
            return newList;
        } else {
            int quick = 1;
            QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<ProductionPlan>();
            queryWrapper.eq("ppquick", quick);
            List<ProductionPlan> productionPlans = productionPlanMapper.selectList(queryWrapper);
            for (ProductionPlan productionPlanGoToRedis : productionPlans) {
                opsForHash.put("findProductionPlanByQuick", String.valueOf(productionPlanGoToRedis.getPpid()), productionPlanGoToRedis);
            }
            return productionPlans;
        }

    }

    @Override
    public List<ProductionPlan> findProductionPlanByNoQuick() {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
//        从缓存读取数据
        List<Object> findProductionPlanByNoQuick = opsForHash.values("findProductionPlanByNoQuick");
//        数据类型转化
        List<ProductionPlan> newList = new ArrayList<ProductionPlan>();
        for (Object object : findProductionPlanByNoQuick) {
            newList.add((ProductionPlan) object);
        }
//        判断是否查询数据库
        if (!newList.isEmpty()) {
            return newList;
        } else {
            int quick = 0;
            QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<ProductionPlan>();
            queryWrapper.eq("ppquick", quick);
            List<ProductionPlan> productionPlans = productionPlanMapper.selectList(queryWrapper);
            for (ProductionPlan productionPlanGoToRedis : productionPlans) {
                opsForHash.put("findProductionPlanByNoQuick", String.valueOf(productionPlanGoToRedis.getPpid()), productionPlanGoToRedis);
            }
            return productionPlans;
        }

    }

    @Override
    public void changeProductionPlan(ProductionPlan productionPlan) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        productionPlanMapper.updateById(productionPlan);
        if (productionPlan.getPpquick() == 0) {
            opsForHash.put("findProductionPlanByNoQuick", String.valueOf(productionPlan.getPpid()), productionPlan);
        } else {
            opsForHash.put("findProductionPlanByQuick", String.valueOf(productionPlan.getPpid()), productionPlan);
        }
    }

    @Override
    public NaohMaterial calculationNaohMaterial(ProductionPlan productionPlan) {
        BigDecimal bigPpnum=new BigDecimal(String.valueOf(productionPlan.getPpnum()));
        BigDecimal bigPpsp=new BigDecimal(String.valueOf(productionPlan.getPpsp()));
        BigDecimal totalNum=bigPpnum.multiply(bigPpsp);
        NaohMaterial naohMaterial=new NaohMaterial();
        naohMaterial.setNacl(totalNum.divide(new BigDecimal("20"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("30")).intValue());
        naohMaterial.setH2o(totalNum.divide(new BigDecimal("20"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("9")).intValue());
        return naohMaterial;
    }

    @Override
    public Nahco3Material calculationNahco3Material(ProductionPlan productionPlan) {
        BigDecimal bigPpnum=new BigDecimal(String.valueOf(productionPlan.getPpnum()));
        BigDecimal bigPpsp=new BigDecimal(String.valueOf(productionPlan.getPpsp()));
        BigDecimal totalNum=bigPpnum.multiply(bigPpsp);
        Nahco3Material nahco3Material=new Nahco3Material();
        nahco3Material.setCo2(totalNum.divide(new BigDecimal("84"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("22")).intValue());
        nahco3Material.setH2o(totalNum.divide(new BigDecimal("84"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("9")).intValue());
        nahco3Material.setNa2co3(totalNum.divide(new BigDecimal("84"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("53")).intValue());
        return nahco3Material;
    }

    @Override
    public Nh3Material calculationNh3Material(ProductionPlan productionPlan) {
        BigDecimal bigPpnum=new BigDecimal(String.valueOf(productionPlan.getPpnum()));
        BigDecimal bigPpsp=new BigDecimal(String.valueOf(productionPlan.getPpsp()));
        BigDecimal totalNum=bigPpnum.multiply(bigPpsp);
        Nh3Material nh3Material=new Nh3Material();
        nh3Material.setN2(totalNum.divide(new BigDecimal("17"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("14")).intValue());
        nh3Material.setH2(totalNum.divide(new BigDecimal("17"),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("3")).intValue());
        return nh3Material;
    }
}
